//
//  EmptyView.swift
//  GreenLight
//
//  Created by ZWJY on 2022/2/17.
//

import UIKit
import SnapKit

// 空页面
class EmptyView: UIView {

    /// 占位图
    private lazy var icon: UIImageView = {
        let img = UIImageView.init()
        return img
    }()

    /// 描述
    private lazy var desp: UILabel = {
        let lab = UILabel.init(text: nil, textColor: .gray, font: UIFont.font14)
            .pg_numberOfLines(2)
            .pg_textAlignment(.center)
        return lab
    }()

    /// 重新加载
    private lazy var reloadBtn: UIButton = {
        let btn = UIButton.init(title: "重新加载", titleColor: .gray, font: UIFont.font16)
            .pg_cornerRadius(adapt_size(18))
            .pg_border(1, .gray)
            .pg_isHidden(true)
        return btn
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        addViews()
        addContraints()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // 配置占位图
    func configEmptyView(emptyDesp: String?,
                         emptyIcon: UIImage?,
                         target: Any?,
                         action: Selector? = nil){
        
        desp.text = emptyDesp
        icon.image = emptyIcon
        
        guard let action = action else {
            reloadBtn.isHidden = true
            return
        }
        reloadBtn.isHidden = false
        reloadBtn.addTarget(target, action: action, for: .touchUpInside)
    }
}

extension EmptyView {

    private func addViews() {
        self.addSubview(icon)
        self.addSubview(desp)
        self.addSubview(reloadBtn)
    }

    private func addContraints() {
        
        let topM: CGFloat = (UIDevice.width - adapt_size(190))/2.0 - adapt_size(50)

        icon.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(topM)
            make.width.height.equalTo(adapt_size(128))
        }

        desp.snp.makeConstraints { make in
            make.top.equalTo(icon.snp.bottom)
            make.left.equalTo(adapt_size(20))
            make.right.equalTo(-adapt_size(20))
        }

        reloadBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(desp.snp.bottom).offset(adapt_size(10))
            make.width.equalTo(adapt_size(100))
            make.height.equalTo(adapt_size(36))
        }
    }
}


